<script lang="ts" setup>
import { ref, watch, reactive } from "vue"
import { type getRegulator } from "@/api/personnel/personnel-list/types/index"
import { getRegulatorDataApi, updateRegulatorDataApi } from "@/api/personnel/personnel-list/index"
import { usePagination } from "@/hooks/usePagination"
import { ElMessage, type FormInstance } from "element-plus"
import { Search, Refresh } from "@element-plus/icons-vue"
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

defineOptions({
  name: "regulator"
})

// 页面加载优化
const loading = ref(true)
// 查 列表数据
// #region
const tableData = ref<getRegulator[]>([])
const tableTotal = ref<number>()
const tableDataTime = ref()
// #region 搜索
const searchData = reactive({
  team: "",
  job: ""
})
const getAddRegulator = () => {
  getRegulatorDataApi({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    team: searchData.team,
    job: searchData.job,
    state: "0"
  })
    .then((res: any) => {
      tableData.value = res.data.taskList
      tableDataTime.value = res.time
      tableTotal.value = res.data.total
      console.log(tableData.value, "页面所有数据")
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
// #endregion

// 点击审核通过与不通过
// #region
const handleTG = (res: any) => {
  res.state = "1"
  // 修改状态码
  updateRegulatorDataApi({
    state: res.state,
    id: res.id,
    message: ""
  }).then(() => {
    console.log("修改成功")
    getAddRegulator()
  })
  ElMessage.success("审核成功!")
}
const handleNTG = (res: any) => {
  res.state = "4"
  // 修改状态码
  updateRegulatorDataApi({
    state: res.state,
    id: res.id,
    message: ""
  }).then(() => {
    console.log("修改成功")
    getAddRegulator()
  })
  ElMessage.success("操作成功!")
}
// #endregion

// 时间转换
function timestampToTime(timestamp: any) {
  // 时间戳为10位需*1000，时间戳为13位不需乘1000
  const date = new Date(timestamp)
  const Y = date.getFullYear() + "-"
  const M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-"
  const D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " "
  return Y + M + D
}

const searchFormRef = ref<FormInstance | null>(null)
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getAddRegulator()
  }
  paginationData.currentPage = 1
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getAddRegulator()
  }
  paginationData.currentPage = 1
}
// #endregion
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getAddRegulator, { immediate: true })
</script>

<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="team" label="归属团队">
          <el-input v-model.trim="searchData.team" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="job" label="职位">
          <el-input v-model.trim="searchData.job" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card-center" v-loading="loading">
      <div class="table-center">
        <el-table :data="tableData">
          <!-- 用插槽替换时间戳转换为日期 -->
          <el-table-column label="日期" align="center">
            <template #default="scope">
              {{ timestampToTime(scope.row.create) }}
            </template>
          </el-table-column>
          <el-table-column prop="team" label="归属团队" align="center" />
          <el-table-column prop="job" label="职位" align="center" />
          <el-table-column prop="num" label="人数" align="center" />
          <el-table-column prop="education" label="学历" align="center" />
          <el-table-column prop="exp" label="工作经验" align="center" />
          <el-table-column prop="sex" label="性别" align="center" />
          <el-table-column label="年龄" align="center">
            <template #default="scope"> {{ scope.row.minAge }}-{{ scope.row.maxAge }} </template>
          </el-table-column>
          <el-table-column prop="technology" label="专业要求" align="center" />
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="handleTG(scope.row)">通过</el-button>
              <el-button link type="primary" size="small" @click="handleNTG(scope.row)">不通过</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :page-size="paginationData.pageSize"
          :total="tableTotal"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}
.table-center {
  margin-bottom: 20px;
}
.pagination {
  display: flex;

  justify-content: flex-end;
}
</style>
